<template>
  <div class="app-container customer-div">
    <el-tabs v-model="activeName">
      <el-tab-pane label="查价记录" name="first">
        <el-form :inline="true" :model="listQuery" size="mini" label-width="100px">
          <el-form-item label="发货地：">
            <el-cascader
              clearable
              v-model="listQuery.sendAddress"
              :props="{ value: 'areaName',label:'areaName',children:'areaChildList' }"
              :options="options"></el-cascader>
          </el-form-item>
          <el-form-item label="收货地：">
            <el-cascader
              clearable
              v-model="listQuery.receiveAddress"
              :props="{ value: 'areaName',label:'areaName',children:'areaChildList' }"
              :options="options"></el-cascader>
          </el-form-item>

          <el-form-item label="查询日期：">
            <el-date-picker
              v-model="listQuery.inquiryDate"
              type="daterange"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </el-form-item>

          <el-form-item>
            <el-button
              @click="handleSearchList()"
              type="primary">
              查询
            </el-button>
          </el-form-item>
        </el-form>

        <el-table
          :header-cell-style="{'text-align':'center'}"
          v-loading="listLoading"
          :data="tableData"
          border
          style="width: 100%; margin-top: 20px">
          <el-table-column  label="业务员" min-width="100" align="center">
            <template slot-scope="scope">
                  {{scope.row.inquiryUserName}}
            </template>
          </el-table-column>
          <el-table-column
            label="发货地"
            min-width="200">
            <template slot-scope="scope">
              {{scope.row.sendAddress}}
            </template>
          </el-table-column>
          <el-table-column
            label="收货地"
            min-width="200">
            <template slot-scope="scope">
              {{scope.row.receiveAddress}}
            </template>
          </el-table-column>
          <el-table-column
            label="数量"
            min-width="100"
            align="center">
            <template slot-scope="scope">
              {{scope.row.inquiryNum}}
            </template>
          </el-table-column>
          <el-table-column
            label="运满满报价"
            min-width="100"
            align="center">
            <template slot-scope="scope">
              {{scope.row.ymmAmount}}
            </template>
          </el-table-column>
          <el-table-column
            label="运满满单价"
            min-width="100"
            align="center">
            <template slot-scope="scope">
              {{scope.row.ymmPrice}}
            </template>
          </el-table-column>
          <el-table-column
            label="加价比例"
            min-width="100"
            align="center">
            <template slot-scope="scope">
              {{scope.row.priceRate}}
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="业务员报价"
            min-width="150">
            <template slot-scope="scope">
              {{scope.row.offerAmount}}
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="业务员报价单价"
            min-width="100">
            <template slot-scope="scope">
              {{scope.row.offerPrice}}
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="查询时间"
            min-width="150">
            <template slot-scope="scope">
              {{scope.row.inquiryTime}}
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination-container" >
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            layout="total, sizes,prev, pager, next,jumper"
            :current-page.sync="listQuery.pageNum"
            :page-size="listQuery.pageSize"
            :page-sizes="[10,15,20]"
            :total="count">
          </el-pagination>
        </div>
      </el-tab-pane>
      <el-tab-pane label="比例设置" name="second">
        <el-form :inline="true" :model="form" size="mini" label-width="120px" style="margin-top:20px" :rules="rules" ref="rateform">
          <el-form-item label="通用比例设置：" prop="priceRate">
                <el-input style="width:100px" v-model="form.priceRate"></el-input> <span>%</span>
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="btnLoading"
              @click="savePriceRate()"
              type="primary">
              保存
            </el-button>
          </el-form-item>
        </el-form>
        <div>
            <span style="color: #606266;font-size:12px;padding:0 5px;">特殊比例设置：</span>
            <el-button @click="showPop('add',null)" type="primary" size="small">新增</el-button>
        </div>
        <el-table
          :header-cell-style="{'text-align':'center'}"
          v-loading="listLoading2"
          :data="tableData2"
          border
          style="width: 600px!important; margin-top: 20px">
          <el-table-column
            label="发货地"
            min-width="200">
            <template slot-scope="scope">
                {{scope.row.sendProvince}}{{scope.row.sendCity}}{{scope.row.sendArea}}
            </template>
          </el-table-column>
          <el-table-column
            label="收货地"
            min-width="200">
            <template slot-scope="scope">
              {{scope.row.receiveProvince}}{{scope.row.receiveCity}}{{scope.row.receiveArea}}
            </template>
          </el-table-column>

          <el-table-column
            label="加价比例"
            min-width="100"
            align="center">
            <template slot-scope="scope">
              {{scope.row.priceRate}}%
            </template>
          </el-table-column>

          <el-table-column
            label="操作"
            min-width="100"
            align="center">
            <template slot-scope="scope">
                <el-button type="text" size="small" @click="showPop('edit',scope.row)">修改</el-button>
                <el-button type="text" size="small" @click="delItem(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination-container" >
          <el-pagination
            background
            @size-change="handleSizeChange2"
            @current-change="handleCurrentChange2"
            layout="total, sizes,prev, pager, next,jumper"
            :current-page.sync="listQuery2.pageNum"
            :page-size="listQuery2.pageSize"
            :page-sizes="[10,15,20]"
            :total="count">
          </el-pagination>
        </div>

      </el-tab-pane>
    </el-tabs>

    <!--特殊比例设置-->
    <el-dialog
      :close-on-click-modal="false"
      title="特殊比例设置"
      :visible.sync="dialogVisible"
      width="500px">
      <el-form :model="setForm" size="small" ref="setForm" label-width="80px" :rules="rules2">
          <el-form-item label="发货地" prop="sendAddress">
            <el-cascader
              clearable
              v-model="setForm.sendAddress"
              :props="{ value: 'areaName',label:'areaName',children:'areaChildList' }"
              :options="options"></el-cascader>
          </el-form-item>
          <el-form-item label="收货地" prop="receiveAddress">
            <el-cascader
              v-if="setForm.configId === 0"
              clearable
              v-model="setForm.receiveAddress"
              :props="{ value: 'areaName',label:'areaName',children:'areaChildList' ,multiple: true, checkStrictly: true }"
              :options="options"></el-cascader>
            <el-cascader
              v-else
              clearable
              v-model="setForm.receiveAddress"
              :props="{ value: 'areaName',label:'areaName',children:'areaChildList' , checkStrictly: true}"
              :options="options"></el-cascader>
          </el-form-item>
          <el-form-item label="加价比例" prop="priceRate">
              <el-input style="width:100px" v-model="setForm.priceRate"></el-input><span style="padding-left:10px">%</span>
          </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible=false">取 消</el-button>
          <el-button type="primary" @click="handleConfirm" :loading="btnLoading2">确 认</el-button>
      </span>
    </el-dialog>

  </div>
</template>
<script>
  import {getCustomerOption,getUserOption,myDeptList,saleOrderList,getOrderStatList,getOrderUsersList,getKingInfo,customersAjaxEdit,getAddressList} from '@/api/partner'
  import {formatDate} from '@/utils/date';
  import store from '@/store'
  import { mapGetters } from 'vuex'
  import {configDel, configList, configSet, defaultPriceRateSet, getIinquiryList} from "../../api/partner";
  const defaultListQuery={
    pageNum: 1,
    pageSize: 10,
    "inquiryDate":[],
    "sendAddress":[],
    "receiveAddress":[],
  };

  const defaultListQuery2={
    pageNum: 1,
    pageSize: 10,
  };
  const defaultForm={
    priceRate:''
  };
  const defaultSetForm = {
    "configId" : 0,
    "sendAddress" : [],
    "receiveAddress" : [],
    "priceRate" : ''
  }
  export default {
    name: "index",
    data() {
      return {
        listLoading: false,
        tableData: [],
        listQuery: JSON.parse(JSON.stringify(defaultListQuery)),
        count: 0,

        listLoading2: false,
        tableData2: [],
        count2: 0,
        listQuery2: JSON.parse(JSON.stringify(defaultListQuery2)),
        activeName:'first',
        options:[],
        dialogVisible:false,
        btnLoading:false,
        form:JSON.parse(JSON.stringify(defaultForm)),
        rules: {
          priceRate:[{required: true, trigger: 'blur',message: '此项必填'}],
        },

        setForm:JSON.parse(JSON.stringify(defaultSetForm)),
        btnLoading2: false,
        rules2: {
          sendAddress:[{required: true, trigger: 'change',message: '此项必填'}],
          receiveAddress:[{required: true, trigger: 'change',message: '此项必填'}],
          priceRate:[{required: true, trigger: 'blur',message: '此项必填'}],
        },
      }
    },
    computed: {
      ...mapGetters([
        'name',
        'mobile',
        'userId',
        'isMange',
      ]),


    },
    created() {
        this.getAddressListData();
        this.getList();
        this.getList2()

    },

    filters: {},
    methods: {
      delItem(item){
        this.$confirm('确认删除吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          configDel({configId:item.configId}).then(()=>{
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.handleSearchList2()
          })

        }).catch(() => {

        });
      },
      showPop(type,item){
        this.dialogVisible = true;
        if(type=='add'){
          this.setForm = JSON.parse(JSON.stringify(defaultSetForm))
        }else{
          this.setForm.priceRate = item.priceRate;
          this.setForm.sendAddress = [item.sendProvince,item.sendCity,item.sendArea]
          this.setForm.receiveAddress = [item.receiveProvince,item.receiveCity];
          if(item.receiveArea){
            this.setForm.receiveAddress.push(item.receiveArea)
          }
          this.setForm.configId = item.configId;
          console.log(this.setForm)
        }
      },
      savePriceRate(){
        this.$refs.rateform.validate((valid) => {
          if (valid) {
            this.btnLoading = true;
            let data = JSON.parse(JSON.stringify(this.form));
            defaultPriceRateSet(data).then((res)=>{
              this.btnLoading = false;
              this.getList2()
            }).catch(()=>{
              this.btnLoading = false;
            })
          }
        })
      },
      //获取省市区列表
      getAddressListData(){
        getAddressList().then((res)=>{
          this.options = res;
        })
      },

      handleClose(){
        this.dialogVisible = false;
      },

      handleConfirm(){
        this.$refs.setForm.validate((valid) => {
          if (valid) {
            this.btnLoading2 = true;
            let data = JSON.parse(JSON.stringify(this.setForm));
            data.sendAddress = data.sendAddress.join("_");
            if(data.configId ===0){
              data.receiveAddress = [];
              this.setForm.receiveAddress.forEach((el)=>{
                data.receiveAddress.push(el.join("_"))
              });
            }else{
              data.receiveAddress = [data.receiveAddress.join("_")];
            }
            configSet(data).then((res)=>{
              this.handleClose()
              this.listQuery2.pageNum = 1;
              this.getList2();
              this.btnLoading2 = false;
            }).catch(()=>{
              this.btnLoading2 = false;
            })
          }
        })
      },
      //获取列表
      getList() {
        this.listLoading = true;
        let data = {};
        data = JSON.parse(JSON.stringify(this.listQuery))
        data.sendAddress = data.sendAddress.join("");
        data.receiveAddress = data.receiveAddress.join("");
        getIinquiryList(data).then(response => {
          this.tableData = response.list;
          this.count = response.count;
          this.listLoading = false;
        }).catch(()=>{
          this.listLoading = false;
        });
      },
      handleSizeChange(val){
        this.listQuery.pageNum = 1;
        this.listQuery.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val){
        this.listQuery.pageNum = val;
        this.getList();
      },
      handleSearchList(){
        this.listQuery.pageNum = 1;
        this.getList();
      },

      //获取列表
      getList2() {
        this.listLoading2 = true;
        let data = {};
        data = JSON.parse(JSON.stringify(this.listQuery2))
        configList(data).then(response => {
          this.tableData2 = response.list;
          this.count2 = response.count;
          this.form.priceRate = response.defaultPriceRate;
          this.listLoading2 = false;
        }).catch(()=>{
          this.listLoading2 = false;
        });
      },
      handleSizeChange2(val){
        this.listQuery2.pageNum = 1;
        this.listQuery2.pageSize = val;
        this.getList2();
      },
      handleCurrentChange2(val){
        this.listQuery2.pageNum = val;
        this.getList2();
      },
      handleSearchList2(){
        this.listQuery2.pageNum = 1;
        this.getList2();
      },
    },
  }
</script>

<style scoped lang="scss">



</style>
